package com.ruoyi.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.weixin.Exception.BussnissExecption;
import com.ruoyi.weixin.anno.ILog;
import com.ruoyi.weixin.entity.Account;
import com.ruoyi.weixin.entity.AccountTab;
import com.ruoyi.weixin.entity.Fam;
import com.ruoyi.weixin.mapper.AccountTabMapper;
import com.ruoyi.weixin.mapper.AccountsMapper;
import com.ruoyi.weixin.mapper.FamMapper;
import com.ruoyi.weixin.service.AccountTabService;
import com.ruoyi.weixin.service.AccountsService;
import com.ruoyi.weixin.service.TabbarService;
import com.ruoyi.weixin.untils.PageListUtils;
import com.ruoyi.weixin.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (Account)表服务实现类
 *
 * @author makejava
 * @since 2024-01-02 17:19:56
 */
@Service("accountService")
public class AccountsServiceImpl extends ServiceImpl<AccountsMapper, Account> implements AccountsService {

    @Autowired
    AccountsMapper accountsMapper;

    @Autowired
    AccountTabMapper tabMapper;

    @Autowired
    FamMapper famMapper;

    @Override
    @Transactional
    public Map<String, Object> accountList(AccountPararm accountPararm) throws ParseException {
        Page<Total> page = new Page<>(accountPararm.getPageNum(), accountPararm.getPageSize());
        HashMap<String, Object> map = new HashMap<>();

        MouthVo mouthVo = accountsMapper.getMounth(accountPararm);
        IPage<Total> totalIPage = accountsMapper.getDay(page, accountPararm);
        List<Total> totalList = totalIPage.getRecords();
        List<Account> accountList = null;
        for (Total total : totalList) {
            accountList = accountsMapper.getAccount(total.getDate(), accountPararm.getUserId());
            total.setAccountList(accountList);
        }
        totalIPage.setRecords(totalList);
        map.put("mouthVo", mouthVo);
        map.put("totalIPage", totalIPage);
        return map;
    }

    @Override
    public Page<Account> selectPaihang(Paihang paihang) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Account> page = new Page<>(paihang.getPageNum(), paihang.getPageSize());
        IPage<Account> parhangAccount = accountsMapper.selectPaihang(page, paihang);
        return page;
    }

    @Override
    public Map<String, Object> yearAcount(Paihang paihang) {
        HashMap<String, Object> map = new HashMap<>();
        float num = 0l;
        float jian = 0l;
        List<MouthVo> yearAcountAccount = accountsMapper.yearAcount(paihang);
        List<String> years = accountsMapper.getyearList(paihang.getUserId());
        for (MouthVo mouthVo : yearAcountAccount) {
            jian += mouthVo.getJian();
            num += mouthVo.getNum();
        }
        map.put("years", years);
        map.put("jian", jian);
        map.put("num", num);
        map.put("year", yearAcountAccount);
        return map;
    }

    @Override
    public List<AccountTab> accountTbList(String type) {
        List<AccountTab> accountTabs = tabMapper.selectList(new LambdaQueryWrapper<AccountTab>().eq(AccountTab::getType, type));
        return accountTabs;
    }

    @Override
    public String getCode(String userId) {
        String substring = UUID.randomUUID().toString().substring(0, 15);
        String code = userId + '_' + substring + '.';
        return code;
    }

    @Override
    public int checkCode(String userId, String code) {
        String[] parts = code.split("_");
        Integer jiaUserId = 0;
        if (parts.length > 1) {
            if (code.length() != userId.length() + 17) {
                throw new BussnissExecption("邀请码错误");
            }
            jiaUserId = Integer.parseInt(parts[0]);
            if (userId.equals(parts[0])) {
                throw new BussnissExecption("不能输入自己的邀请码");
            }
            LambdaQueryWrapper<Fam> wrapper = new LambdaQueryWrapper<>();
            Fam si = new Fam(null, Integer.parseInt(userId), jiaUserId);
            Fam gong = famMapper.selectOne(wrapper.eq(Fam::getUserId, jiaUserId));
            if (gong == null) {
                gong = new Fam(null, jiaUserId, jiaUserId);
                famMapper.insert(gong);
            }
            wrapper.clear();
            if ( famMapper.selectOne(wrapper.eq(Fam::getUserId,userId))!=null){
                throw new BussnissExecption("您已经加入别的家庭记账！！");
            }
            famMapper.insert(si);

            return jiaUserId;
        } else {
            throw new BussnissExecption("邀请码错误");
        }
    }

    @Override
    public int checkUser(String userId) {
        Fam fam = famMapper.selectOne(new LambdaQueryWrapper<Fam>().eq(Fam::getUserId, userId));
        if (fam!=null){
            return fam.getFamId();
        }
        return 0;
    }


}
